package hr.ctg.web.resource;

import hr.ctg.domain.action.TitleAction;
import hr.ctg.domain.model.Mjesto;
import hr.ctg.domain.model.Objekt;
import hr.ctg.domain.model.Otok;
import hr.ctg.domain.model.Park;
import hr.ctg.domain.model.TipObjekta;
import hr.ctg.domain.model.Zupanija;
import hr.ctg.service.dao.MjestoDAO;
import hr.ctg.service.dao.ObjektDAO;
import hr.ctg.service.dao.OtokDAO;
import hr.ctg.service.dao.ParkDAO;
import hr.ctg.service.dao.TipObjektaDAO;
import hr.ctg.service.dao.ZupanijaDAO;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
import org.jboss.seam.international.LocaleSelector;
import org.jboss.seam.log.Log;
import org.jboss.seam.servlet.ContextualHttpServletRequest;
import org.jboss.seam.web.AbstractResource;

@Scope(ScopeType.APPLICATION)
@Name("sitemapTxtResource")
@BypassInterceptors
public class SiteMapTxtResource extends AbstractResource {
	@Logger
	Log log;

	@Override
	public void getResource(final HttpServletRequest req,
			final HttpServletResponse res) throws ServletException, IOException {
		log.error("Loggiram SITEMAP", this);
		new ContextualHttpServletRequest(req) {
			
			@Override
			public void process() throws IOException {
		res.setContentType("text/txt");
		res.setCharacterEncoding("UTF8");
		final MjestoDAO mjestoDAO = (MjestoDAO) Component.getInstance(MjestoDAO.class);
		final OtokDAO otokDAO = (OtokDAO) Component.getInstance(OtokDAO.class);
		final ZupanijaDAO zupanijaDAO = (ZupanijaDAO) Component.getInstance(ZupanijaDAO.class);
		final ObjektDAO objektDAO = (ObjektDAO) Component.getInstance(ObjektDAO.class);
		final TipObjektaDAO tipObjektaDAO = (TipObjektaDAO) Component.getInstance("tipObjektaDAO");
		final ParkDAO parkDAO = (ParkDAO) Component.getInstance("parkDAO");
		final TitleAction titleAction = (TitleAction)Component.getInstance("titleAction");
		
		final LocaleSelector ls = (LocaleSelector) Component.getInstance(LocaleSelector.class);
		
		List<Mjesto> mjesta = null;
		Properties messages = new Properties();
		messages.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("messages_"+ls.getLanguage()+".properties"));
		
		
		
		final PrintWriter out = res.getWriter();
		
		out.write("http://www.crotouristguide.com/main/"+messages.get("url.home")+"\n");
		
		// reccomendations accomodation
		List<TipObjekta> toList = tipObjektaDAO.findAll();
		for (final TipObjekta tipObjekta : toList) {
			out.write("http://www.crotouristguide.com/main/"+messages.get("url.accomodation")+"-"+messages.get(tipObjekta.getIdTipaObjekta()+".plural").toString().toLowerCase()+"-lidTipaObjekta_"+tipObjekta.getIdTipaObjekta()+"\n");
			// lista specijalnih ponuda ovog tipa
			out.write("http://www.crotouristguide.com/main/"+messages.get("url.specialoffers")+"-"+tipObjekta.getIdTipaObjekta()+"\n");
		}
		
		out.write("http://www.crotouristguide.com/main/"+messages.get("url.tourguide")+"-"+messages.get("url.croatia")+"-"+messages.get("url.vacations")+"\n");
		// zupanije
		final List<Zupanija> zupanije = zupanijaDAO.findAll();
		for (int i = 0; i < zupanije.size(); i++) {
			// link na zupaniju
			out.write("http://www.crotouristguide.com/main/"+messages.get("url.tourguide")+"-"+titleAction.getUrlTitle(zupanije.get(i))+"-"+messages.get("url.vacations")+"-z"+zupanije.get(i).getIdZupanje()+"\n");
			
			// link na listu mjesta u zupaniji
			out.write("http://www.crotouristguide.com/main/"+messages.get("url.tourguide")+"-"+titleAction.getUrlTitle(zupanije.get(i))+"-"+messages.get("url.places")+"-z"+zupanije.get(i).getIdZupanje()+"\n");
			// link na listu otoka u zupaniji
			if(otokDAO.findByIdZupanije(zupanije.get(i).getIdZupanje()).size()>0)
			out.write("http://www.crotouristguide.com/main/"+messages.get("url.tourguide")+"-"+titleAction.getUrlTitle(zupanije.get(i))+"-"+messages.get("url.islands")+"-z"+zupanije.get(i).getIdZupanje()+"\n");
			// link na listu objekata odredjenog tipa u zupaniji
			toList = tipObjektaDAO.findByIdZupanije(zupanije.get(i).getIdZupanje());
			for (TipObjekta to : toList) {
				out.write("http://www.crotouristguide.com/main/"+messages.get("url.accomodation")+"-"+titleAction.getUrlTitle(zupanije.get(i),to.getIdTipaObjekta())+"-z"+zupanije.get(i).getIdZupanje()+"_"+to.getIdTipaObjekta()+"\n");
			}
		}
		out.flush();
		// otoci
		out.write("http://www.crotouristguide.com/main/"+messages.get("url.tourguide")+"-"+messages.get("url.croatia")+"-"+messages.get("url.vacations")+"-"+messages.get("url.islands")+"\n");
		final List<Otok> otoci = otokDAO.findAll();
		for (int i = 0; i < otoci.size(); i++) {
			out.write("http://www.crotouristguide.com/main/"+messages.get("url.tourguide")+"-"+titleAction.getUrlTitle(otoci.get(i))+"-"+messages.get("url.vacations")+"-o"+otoci.get(i).getIdOtoka()+"\n");
			// link na listu mjesta na otoku
			if(mjestoDAO.findByIdOtoka(otoci.get(i).getIdOtoka()).size()>0)
			out.write("http://www.crotouristguide.com/main/"+messages.get("url.tourguide")+"-"+titleAction.getUrlTitle(otoci.get(i))+"-"+messages.get("url.places")+"-o"+otoci.get(i).getIdOtoka()+"\n");
			// link na listu objekata odredjenog tipa na otoku
			toList = tipObjektaDAO.findByIdOtoka(otoci.get(i).getIdOtoka());
			for (TipObjekta to : toList) {
				out.write("http://www.crotouristguide.com/main/"+messages.get("url.accomodation")+"-"+titleAction.getUrlTitle(otoci.get(i),to.getIdTipaObjekta())+"-o"+otoci.get(i).getIdOtoka()+"_"+to.getIdTipaObjekta()+"\n");
			}
		}
		out.flush();
		// mjesta
		@SuppressWarnings("unchecked")
		final List<String> abc = (List) Component
				.getInstance("firstMjestaLetters");
		for (final String string : abc) {
			out.write("http://www.crotouristguide.com/main/"+messages.get("url.tourguide")+"-"+messages.get("url.croatia")+"-"+messages.get("url.vacations")+"-"+messages.get("url.places")+"_"+string+"\n");
		}
		mjesta = mjestoDAO.findAll();
		for (int i = 0; i < mjesta.size(); i++) {
			out.write("http://www.crotouristguide.com/main/"+messages.get("url.tourguide")+"-"+titleAction.getUrlTitle(mjesta.get(i))+"-"+messages.get("url.vacations")+"-m"+mjesta.get(i).getIdMjesta()+"\n");
			// link na listu objekata odredjenog tipa u mjestu
			toList = tipObjektaDAO.findByIdMjesta(mjesta.get(i).getIdMjesta());
			for (TipObjekta to : toList) {
				out.write("http://www.crotouristguide.com/main/"+messages.get("url.accomodation")+"-"+titleAction.getUrlTitle(mjesta.get(i),to.getIdTipaObjekta())+"-s"+mjesta.get(i).getIdMjesta()+"_"+to.getIdTipaObjekta()+"\n");
			}
		}
		out.flush();
		// objekti
		out.write("http://www.crotouristguide.com/main/"+messages.get("url.accomodation")+"-"+messages.get("url.croatia")+"\n");
		final List<Objekt> objekti = objektDAO.findAll();
		for (int i = 0; i < objekti.size(); i++) {
			out.write("http://www.crotouristguide.com/main/"+messages.get("url.accomodation")+"-"+titleAction.getUrlTitle(objekti.get(i))+"-s"+objekti.get(i).getIdObjekta()+"\n");
		}
		out.flush();
		
		// parks
		out.write("http://www.crotouristguide.com/main/"+messages.get("url.tourguide")+"-"+messages.get("url.croatia")+"-"+messages.get("url.nacpark")+"\n");
		final List<Park> parkovi = parkDAO.findAll();
		for (int i = 0; i < parkovi.size(); i++) {
			out.write("http://www.crotouristguide.com/main/"+messages.get("url.tourguide")+"-"+messages.get("url.park")+"-"+titleAction.getUrlTitle(parkovi.get(i))+"-p"+parkovi.get(i).getIdParka()+"\n");
		}
		// map
		out.write("http://www.crotouristguide.com/main/"+messages.get("url.map")+"-"+messages.get("url.croatia")+"\n");
		
		out.flush();
		out.close();
			}
		}.run();
	}

	@Override
	public String getResourcePath() {
		return "/sitemaptxt.txt";
	}

}
